package com.ms.game.characterInfo.db;

import java.sql.Date;
import java.util.List;

import com.ms.game.characterInfo.GameLogInfo;
import com.ms.game.characterInfo.message.CharacterInfoResponse;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.zone.BlockType;
import com.ms.service.module.zone.module.BasicZoneModule;
import com.xpec.c4.db.model.basic.SlotBasicGameLogModel;
import com.xpec.c4.db.model.log.basic.BaseGameInfoLogModel;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.BasicServiceModule;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

/**
 * 依照日期查詢遊戲資料
 *
 */
public class SearchCharacterInfoByDateAsync extends TaskAsyncCommand {

	public GUID playerID;
	
	public GUID sessionID;
	
	/**
	 * 日期
	 */
	public Long date;
	
	/**
	 * logTable Class名稱
	 */
	public String logClassName;
	
	/**
	 * 回傳消息
	 */
	public CharacterInfoResponse response;
	
	@Override
	public void asyncExecute() throws Exception {

		response.gameInfoLog.clear();

		Date dateTime = new Date(date);
		// 日期查詢後結果
		List<Object> list = CharacterInfoLogDAO.loadGameInfoByGuidAndDateTime(playerID, dateTime, logClassName);

		for (Object obj : list) {
			
			if(obj instanceof SlotBasicGameLogModel){
				SlotBasicGameLogModel log = (SlotBasicGameLogModel) obj;
				GameLogInfo gameLog = new GameLogInfo(log);
				response.gameInfoLog.add(gameLog);
			}
			else if(obj instanceof BaseGameInfoLogModel){
				BaseGameInfoLogModel log = (BaseGameInfoLogModel) obj;
				GameLogInfo gameLog = new GameLogInfo(log);
				response.gameInfoLog.add(gameLog);
			}
			
		}

		//回傳查詢結果
		response.errorCode = ErrorCode.SUCCESS;
		MessageAction.sendMessage(sessionID, MessageTypes.S_SEARCH_CHARACTER_GAME_INFO_BY_DATE, response);
	}

	@Override
	public void execute() throws Exception {
		BasicServiceModule module = getCurrentModuleServer();
		if (module instanceof BasicZoneModule) {
			BasicZoneModule zoneModule = (BasicZoneModule) module;
			zoneModule.getZone().removeBlockList(playerID, BlockType.SUBKEY_SEARCH_CHARACTER_INFO_BY_DATE);
		}
	}
}
